package com.sec.android.app.myfiles.module.download.providerapps;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.MediaFile;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.AbsProviderImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.download.DownloadFileRecord.DownloadType;

public class BluetoothProviderImp extends AbsSupportAppProviderImp {
    private static final String BLUETOOTH_AUTHORITY = "com.android.bluetooth.opp";
    private static final String BLUETOOTH_BASE = "content://" + BLUETOOTH_AUTHORITY;
    public static final Uri BLUETOOTH_BTOPP_URI = Uri.parse(BLUETOOTH_BASE + "/btopp");

    private static final String FILEID = "_id";
    private static final String PATH = "_data";
    private static final String MIME_TYPE = "mimetype";
    private static final String DIRECTION = "direction";
    private static final String STATUS = "status";
    private static final String SIZE = "total_bytes";
    private static final String DATE_MODIFIED = "timestamp";
    private static final String DESCRIPTION = "device_name";

    private static final String[] PROJECTION = new String[] {
            FILEID, PATH, MIME_TYPE, DIRECTION, STATUS, SIZE, DATE_MODIFIED, DESCRIPTION };

    private int mFileIdIndex;
    private int mPathIndex;
    private int mSizeIndex;
    private int mDateIndex;
    private int mMimeTypeIndex;
    private int mDescriptionIndex;
    private int mStatusIndex;

    public BluetoothProviderImp(Context context, AbsProviderImp provider, SQLiteDatabase db) {
        super(context, provider, db);
    }

    @Override
    protected Cursor getCursor() {
        int nMaxID = getProviderMaxIdEntry();
        Cursor cursor;
        try {
            cursor = mContentResolver.query(BLUETOOTH_BTOPP_URI, PROJECTION, FILEID + ">" + nMaxID, null, null);
        } catch (Exception e) {
            Log.e(this, e.getMessage());
            cursor = null;
        }
        return cursor;
    }

    @Override
    protected ContentValues getContentValues(Cursor cursor) {
        ContentValues ret = null;

        int bluetoothId = cursor.getInt(mFileIdIndex);
        String fullPath = cursor.getString(mPathIndex);
        if (fullPath != null) {

            int status = cursor.getInt(mStatusIndex);

            if(status == AppConstants.DownloadHistoryType.SUCCESS) {
                String path = FileRecord.getPath(fullPath);
                String name = FileRecord.getName(fullPath);
                String extension = FileRecord.getExt(fullPath);
                int fileType = MediaFile.getFileType(fullPath);
                int size = cursor.getInt(mSizeIndex);
                long date = cursor.getLong(mDateIndex);
                String mimeType = cursor.getString(mMimeTypeIndex);
                String description = cursor.getString(mDescriptionIndex);

                ret = new ContentValues();

                ret.put(COL_DownloadId, bluetoothId);
                ret.put(COL_Name, name);
                ret.put(COL_FileType, fileType);
                ret.put(COL_Size, size);
                ret.put(COL_Date, date);
                ret.put(COL_Path, path);
                ret.put(COL_Ext, extension);
                ret.put(COL_MimeType, mimeType);
                ret.put(COL_Source, "");
                ret.put(COL_Description, description);
                ret.put(COL_Status, status);
                ret.put(COL_DownloadBy, DownloadType.DOWNLOAD_BY_BLUETOOTH.ordinal());
            }
        }

        return ret;
    }

    @Override
    protected void setColumnIndex(Cursor cursor) {
        mFileIdIndex = cursor.getColumnIndex(FILEID);
        mPathIndex = cursor.getColumnIndex(PATH);
        mSizeIndex = cursor.getColumnIndex(SIZE);
        mDateIndex = cursor.getColumnIndex(DATE_MODIFIED);
        mMimeTypeIndex = cursor.getColumnIndex(MIME_TYPE);
        mDescriptionIndex = cursor.getColumnIndex(DESCRIPTION);
        mStatusIndex = cursor.getColumnIndex(STATUS);

    }

    @Override
    protected boolean needClearUnnecessaryData() {
        return true;
    }

    @Override
    protected DownloadType getDownloadType() {
        return DownloadType.DOWNLOAD_BY_BLUETOOTH;
    }

}
